﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;

namespace GUI

{
    public partial class ThongKe_PhieuDatCom : Form
    {
        public ThongKe_PhieuDatCom()
        {
            InitializeComponent();
            cbbTrangThai.Items.Add("Chưa giao");
            cbbTrangThai.Items.Add("Đã giao");
            txtNgay.Mask = "00/00/0000";
            txtNgay.KeyUp += new KeyEventHandler(txtNgay_KeyUp);
        }

        void msDate_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtNgay.MaskFull)
            {
                try
                {
                    DateTime.ParseExact(txtNgay.Text, "dd/MM/yyyy", null);
                    MessageBox.Show("Ngày hợp lệ");
                }
                catch
                {
                    MessageBox.Show("Ngày không hợp lệ");
                    txtNgay.ResetText();
                }
            }
        }
        private void ThongKe_PhieuDatCom_Load(object sender, EventArgs e)
        {

        }

        private void txtNgay_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtNgay.MaskFull)
            {
                try
                {
                    DateTime.ParseExact(txtNgay.Text, "dd/MM/yyyy", null);
                }
                catch
                {
                    MessageBox.Show("Ngày không hợp lệ");
                    txtNgay.ResetText();
                }
            }
        }

        private void bttCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// /Đưa danh sách vào datagridview
        /// </summary>
        void DuaDSThongKe()
        {
            int loai;
           
            if (CauHinh.chedo == 0)
            {
                loai = 0;
            }
            else
            {
                loai = 1;
            }
            ThongKe_PhieuDatCom_BUS tk = new ThongKe_PhieuDatCom_BUS();
            DataSet dts = new DataSet();
            string t = txtNgay.Text;
            int day = int.Parse(t.Substring(0, 2));
            int month = int.Parse(t.Substring(3, 2));
            int year = int.Parse(t.Substring(6, 4));
            int tinhtrang = cbbTrangThai.SelectedIndex;
            DateTime ngaythang = new DateTime(year, month, day);
            dts = tk.ThongKe_dts(loai, ngaythang, tinhtrang);
            DataTable dt = new DataTable();
            try
            {
                ////Lay tong so phieu dat com
                dt = dts.Tables[0];
                String SoPhieu = dt.Rows[0]["TongPhieu"].ToString();
                txtSoPhieu.Text = SoPhieu;
                ////////////////////////////
                dt = dts.Tables[1];
                dgvThongKe.Rows.Clear();
                //Dua danh sach vao dataview
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string ID_PhieuDatCom = dt.Rows[i]["ID_PhieuDatCom"].ToString();
                    string NgayDat = dt.Rows[i]["NgayDat"].ToString();
                    string NgayGiao = dt.Rows[i]["NgayGiao"].ToString();
                    string GioDat = dt.Rows[i]["GioDat"].ToString();
                    string GioGiao = dt.Rows[i]["GioGiao"].ToString();
                    string ID_NhanVienDat = dt.Rows[i]["ID_NhanVienDat"].ToString();
                    string ID_NhanVienGiao = dt.Rows[i]["ID_NhanVienGiao"].ToString();
                    string ID_KhachHang = dt.Rows[i]["ID_KhachHang"].ToString();
                    string ID_KhuyenMai = dt.Rows[i]["ID_KhuyenMai"].ToString();
                    string GhiChu = dt.Rows[i]["GhiChu"].ToString();
                    string TongTien = dt.Rows[i]["TongTien"].ToString();
                    string TrangThai = dt.Rows[i]["TrangThai"].ToString();
                    dgvThongKe.Rows.Add(ID_PhieuDatCom, NgayDat, NgayGiao, GioDat, GioGiao, ID_NhanVienDat, ID_NhanVienGiao, ID_KhachHang, ID_KhuyenMai, GhiChu, TongTien, TrangThai);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void bttOK_Click(object sender, EventArgs e)
        {
           

        }

        private void ThongKe_PhieuDatCom_Load_1(object sender, EventArgs e)
        {

        }

        private void bttOK_Click_1(object sender, EventArgs e)
        {
            string t = txtNgay.Text;
            int day = int.Parse(t.Substring(0, 2));
            int month = int.Parse(t.Substring(3, 2));
            int year = int.Parse(t.Substring(6, 4));
            //   MessageBox.Show(day.ToString() + month.ToString() + year.ToString());
            //  DateTime ngaythang = DateTime.Parse()
            DateTime ngaythang = new DateTime(year, month, day);
            DuaDSThongKe();
        }


    }
}
